package com.inei.eea2014.controller.activity;

import java.util.ArrayList;
import java.util.List;
import com.inei.eea2014.R;
import com.inei.eea2014.controller.Capitulo2Controller;
import com.inei.eea2014.controller.JustificacionController;
import com.inei.eea2014.controller.MarcoController;
import com.inei.eea2014.model.entity.Capitulo2;
import com.inei.eea2014.model.entity.Justificacion;
import com.inei.eea2014.model.entity.Marco;
import com.inei.eea2014.tools.Mensaje;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableRow;

public class CapituloII_Fragment extends Fragment {

	// componentes
	private View root;
	private TableRow trC2011, trC203, trC204, trC2061, trC2062, trC207, trC208,
			trC209, trC210, trC213;
	private EditText etC2_01, etC2_02, etC2_03, etC2_04, etC2_05, etC2_06,
			etC2_07, etC2_08, etC2_09, etC2_10, etC2_11, etC2_12, etC2_13,
			etC2011, etC203, etC204, etC2061, etC2062, etC207, etC208, etC209,
			etC210, etC213;

	// variables
	private String RUC;
	private String USUARIO;
	private Capitulo2Controller cap2Con;
	private Capitulo2 obj;
	private JustificacionController jusCon;
	private List<Justificacion> lJust;
	private Mensaje m;
	private boolean nuevo = false;
	private boolean nuevoJust = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater
				.inflate(R.layout.fragment_capitulo_ii, container, false);

		return root;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		cap2Con = new Capitulo2Controller(getActivity());
		jusCon = new JustificacionController(getActivity());
		m = new Mensaje(getActivity());

		IniciarComponentes();

		AgregarEventos();

		CargarDatos();
	}

	private void IniciarComponentes() {
		trC2011 = (TableRow) root.findViewById(R.id.trC2011);
		trC203 = (TableRow) root.findViewById(R.id.trC203);
		trC204 = (TableRow) root.findViewById(R.id.trC204);
		trC2061 = (TableRow) root.findViewById(R.id.trC2061);
		trC2062 = (TableRow) root.findViewById(R.id.trC2062);
		trC207 = (TableRow) root.findViewById(R.id.trC207);
		trC208 = (TableRow) root.findViewById(R.id.trC208);
		trC209 = (TableRow) root.findViewById(R.id.trC209);
		trC210 = (TableRow) root.findViewById(R.id.trC210);
		trC213 = (TableRow) root.findViewById(R.id.trC213);

		Ocultar();

		etC2_01 = (EditText) root.findViewById(R.id.etC2_01);
		etC2_02 = (EditText) root.findViewById(R.id.etC2_02);
		etC2_03 = (EditText) root.findViewById(R.id.etC2_03);
		etC2_04 = (EditText) root.findViewById(R.id.etC2_04);
		etC2_05 = (EditText) root.findViewById(R.id.etC2_05);
		etC2_06 = (EditText) root.findViewById(R.id.etC2_06);
		etC2_07 = (EditText) root.findViewById(R.id.etC2_07);
		etC2_08 = (EditText) root.findViewById(R.id.etC2_08);
		etC2_09 = (EditText) root.findViewById(R.id.etC2_09);
		etC2_10 = (EditText) root.findViewById(R.id.etC2_10);
		etC2_11 = (EditText) root.findViewById(R.id.etC2_11);
		etC2_12 = (EditText) root.findViewById(R.id.etC2_12);
		etC2_13 = (EditText) root.findViewById(R.id.etC2_13);
		etC2011 = (EditText) root.findViewById(R.id.etC2011);
		etC203 = (EditText) root.findViewById(R.id.etC203);
		etC204 = (EditText) root.findViewById(R.id.etC204);
		etC2061 = (EditText) root.findViewById(R.id.etC2061);
		etC2062 = (EditText) root.findViewById(R.id.etC2062);
		etC207 = (EditText) root.findViewById(R.id.etC207);
		etC208 = (EditText) root.findViewById(R.id.etC208);
		etC209 = (EditText) root.findViewById(R.id.etC209);
		etC210 = (EditText) root.findViewById(R.id.etC210);
		etC213 = (EditText) root.findViewById(R.id.etC213);
	}

	private void Ocultar() {
		trC2011.setVisibility(View.GONE);
		trC203.setVisibility(View.GONE);
		trC204.setVisibility(View.GONE);
		trC2061.setVisibility(View.GONE);
		trC2062.setVisibility(View.GONE);
		trC207.setVisibility(View.GONE);
		trC208.setVisibility(View.GONE);
		trC209.setVisibility(View.GONE);
		trC210.setVisibility(View.GONE);
		trC213.setVisibility(View.GONE);
	}

	private void AgregarEventos() {
		EventoClave06(etC2_01);
		EventoClave06(etC2_02);
		EventoClave06(etC2_03);
		EventoClave06(etC2_04);
		EventoClave06(etC2_05);

		EventoClave13(etC2_07);
		EventoClave13(etC2_08);
		EventoClave13(etC2_09);
		EventoClave13(etC2_10);
		EventoClave13(etC2_11);
		EventoClave13(etC2_12);
	}

	private void EventoClave06(EditText et) {
		et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				int n01 = Integer.parseInt(etC2_01.getText().toString().trim()
						.length() == 0 ? "0" : etC2_01.getText().toString());
				int n02 = Integer.parseInt(etC2_02.getText().toString().trim()
						.length() == 0 ? "0" : etC2_02.getText().toString());
				int n03 = Integer.parseInt(etC2_03.getText().toString().trim()
						.length() == 0 ? "0" : etC2_03.getText().toString());
				int n04 = Integer.parseInt(etC2_04.getText().toString().trim()
						.length() == 0 ? "0" : etC2_04.getText().toString());
				int n05 = Integer.parseInt(etC2_05.getText().toString().trim()
						.length() == 0 ? "0" : etC2_05.getText().toString());
				int sum06 = n01 + n02 + n03 + n04 + n05;

				etC2_06.setText((sum06) + "");
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	private void EventoClave13(EditText et) {
		et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				int n07 = Integer.parseInt(etC2_07.getText().toString().trim()
						.length() == 0 ? "0" : etC2_07.getText().toString());
				int n08 = Integer.parseInt(etC2_08.getText().toString().trim()
						.length() == 0 ? "0" : etC2_08.getText().toString());
				int n09 = Integer.parseInt(etC2_09.getText().toString().trim()
						.length() == 0 ? "0" : etC2_09.getText().toString());
				int n10 = Integer.parseInt(etC2_10.getText().toString().trim()
						.length() == 0 ? "0" : etC2_10.getText().toString());
				int n11 = Integer.parseInt(etC2_11.getText().toString().trim()
						.length() == 0 ? "0" : etC2_11.getText().toString());
				int n12 = Integer.parseInt(etC2_12.getText().toString().trim()
						.length() == 0 ? "0" : etC2_12.getText().toString());

				etC2_13.setText((n07 + n08 + n09 + n10 + n11 + n12) + "");
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	private void CargarDatos() {
		RUC = getArguments().getString("RUC");
		USUARIO = getArguments().getString("USUARIO");

		obj = new Capitulo2(RUC, USUARIO);

		obj = cap2Con.Cap02Obtener(obj);
		if (obj != null) {
			etC2_01.setText(obj.getC0002_001_01_N() + "");
			etC2_02.setText(obj.getC0002_002_01_N() + "");
			etC2_03.setText(obj.getC0002_003_01_N() + "");
			etC2_04.setText(obj.getC0002_004_01_N() + "");
			etC2_05.setText(obj.getC0002_005_01_N() + "");
			etC2_06.setText(obj.getC0002_006_01_N() + "");
			etC2_07.setText(obj.getC0002_007_01_N() + "");
			etC2_08.setText(obj.getC0002_008_01_N() + "");
			etC2_09.setText(obj.getC0002_009_01_N() + "");
			etC2_10.setText(obj.getC0002_010_01_N() + "");
			etC2_11.setText(obj.getC0002_011_01_N() + "");
			etC2_12.setText(obj.getC0002_012_01_N() + "");
			etC2_13.setText(obj.getC0002_013_01_N() + "");
		}

		Justificacion tmp = new Justificacion(RUC, USUARIO, "2");
		lJust = jusCon.JustificacionObtenerPorRuc(tmp);
		if (lJust == null) {
			nuevoJust = true;
			lJust = new ArrayList<Justificacion>();
			for (int i = 0; i < 10; i++) {
				Justificacion just = new Justificacion((i + 1), RUC, USUARIO,
						"2", "1");
				lJust.add(i, just);
			}
			lJust.get(0).setC0JUS_001_04_N("MP_02O01");
			lJust.get(1).setC0JUS_001_04_N("MP_02O02");
			lJust.get(2).setC0JUS_001_04_N("MP_02O03");
			lJust.get(3).setC0JUS_001_04_N("MP_02O04");
			lJust.get(4).setC0JUS_001_04_N("MP_02R01");
			lJust.get(5).setC0JUS_001_04_N("MP_02R01_I");
			lJust.get(6).setC0JUS_001_04_N("MP_02R02");
			lJust.get(7).setC0JUS_001_04_N("MP_02R02_I");
			lJust.get(8).setC0JUS_001_04_N("MP_02P01");
			lJust.get(9).setC0JUS_001_04_N("MP_02P02");
		} else {
			if (lJust.get(0).getC0JUS_001_03_N() != null) {
				etC2061.setText(lJust.get(0).getC0JUS_001_03_N());
			}
			if (lJust.get(1).getC0JUS_001_03_N() != null) {
				etC213.setText(lJust.get(1).getC0JUS_001_03_N());
			}
			if (lJust.get(2).getC0JUS_001_03_N() != null) {
				etC209.setText(lJust.get(2).getC0JUS_001_03_N());
			}
			if (lJust.get(3).getC0JUS_001_03_N() != null) {
				etC210.setText(lJust.get(3).getC0JUS_001_03_N());
			}
			if (lJust.get(4).getC0JUS_001_03_N() != null) {
				etC207.setText(lJust.get(4).getC0JUS_001_03_N());
			}
			if (lJust.get(5).getC0JUS_001_03_N() != null) {
				etC2011.setText(lJust.get(5).getC0JUS_001_03_N());
			}
			if (lJust.get(6).getC0JUS_001_03_N() != null) {
				etC208.setText(lJust.get(6).getC0JUS_001_03_N());
			}
			if (lJust.get(7).getC0JUS_001_03_N() != null) {
				etC203.setText(lJust.get(7).getC0JUS_001_03_N());
			}
			if (lJust.get(8).getC0JUS_001_03_N() != null) {
				etC2062.setText(lJust.get(8).getC0JUS_001_03_N());
			}
			if (lJust.get(9).getC0JUS_001_03_N() != null) {
				etC204.setText(lJust.get(9).getC0JUS_001_03_N());
			}

			ValidarCap();
		}
	}

	public void ValidarCap() {
		int c01 = Integer
				.parseInt(etC2_01.getText().toString().length() == 0 ? "0"
						: etC2_01.getText().toString());
		int c02 = Integer
				.parseInt(etC2_02.getText().toString().length() == 0 ? "0"
						: etC2_02.getText().toString());
		int c03 = Integer
				.parseInt(etC2_03.getText().toString().length() == 0 ? "0"
						: etC2_03.getText().toString());
		int c04 = Integer
				.parseInt(etC2_04.getText().toString().length() == 0 ? "0"
						: etC2_04.getText().toString());
		int c06 = Integer
				.parseInt(etC2_06.getText().toString().length() == 0 ? "0"
						: etC2_06.getText().toString());
		int c07 = Integer
				.parseInt(etC2_07.getText().toString().length() == 0 ? "0"
						: etC2_07.getText().toString());
		int c08 = Integer
				.parseInt(etC2_08.getText().toString().length() == 0 ? "0"
						: etC2_08.getText().toString());
		int c09 = Integer
				.parseInt(etC2_09.getText().toString().length() == 0 ? "0"
						: etC2_09.getText().toString());
		int c10 = Integer
				.parseInt(etC2_10.getText().toString().length() == 0 ? "0"
						: etC2_10.getText().toString());
		int c13 = Integer
				.parseInt(etC2_13.getText().toString().length() == 0 ? "0"
						: etC2_13.getText().toString());

		if (c07 > 0 && c01 == 0) {
			trC2011.setVisibility(View.VISIBLE);
		} else {
			etC2011.setText("");
			trC2011.setVisibility(View.GONE);
		}
		if (c08 > 0 && (c02 + c03) == 0) {
			trC203.setVisibility(View.VISIBLE);
		} else {
			etC203.setText("");
			trC203.setVisibility(View.GONE);
		}
		if ((c02 + c03 + c04) <= c01) {
			trC204.setVisibility(View.VISIBLE);
		} else {
			etC204.setText("");
			trC204.setVisibility(View.GONE);
		}
		if (c06 == 0) {
			trC2061.setVisibility(View.VISIBLE);
		} else {
			etC2061.setText("");
			trC2061.setVisibility(View.GONE);
		}
		if (c06 <= c13) {
			trC2062.setVisibility(View.VISIBLE);
		} else {
			etC2062.setText("");
			trC2062.setVisibility(View.GONE);
		}
		if (c01 > 0 && c07 == 0) {
			trC207.setVisibility(View.VISIBLE);
		} else {
			etC207.setText("");
			trC207.setVisibility(View.GONE);
		}
		if ((c02 + c03) > 0 && c08 == 0) {
			trC208.setVisibility(View.VISIBLE);
		} else {
			etC208.setText("");
			trC208.setVisibility(View.GONE);
		}
		if (c09 == 0) {
			trC209.setVisibility(View.VISIBLE);
		} else {
			etC209.setText("");
			trC209.setVisibility(View.GONE);
		}
		if (c10 == 0) {
			trC210.setVisibility(View.VISIBLE);
		} else {
			etC210.setText("");
			trC210.setVisibility(View.GONE);
		}
		if (c13 == 0) {
			trC213.setVisibility(View.VISIBLE);
		} else {
			etC213.setText("");
			trC213.setVisibility(View.GONE);
		}
	}

	public void GuardarCap() {
		if (obj == null) {
			nuevo = true;
			obj = new Capitulo2(RUC, USUARIO);
		}

		obj.setC0002_001_01_N(Integer.parseInt(etC2_01.getText().toString()
				.trim().length() == 0 ? "0" : etC2_01.getText().toString()));
		obj.setC0002_002_01_N(Integer.parseInt(etC2_02.getText().toString()
				.trim().length() == 0 ? "0" : etC2_02.getText().toString()));
		obj.setC0002_003_01_N(Integer.parseInt(etC2_03.getText().toString()
				.trim().length() == 0 ? "0" : etC2_03.getText().toString()));
		obj.setC0002_004_01_N(Integer.parseInt(etC2_04.getText().toString()
				.trim().length() == 0 ? "0" : etC2_04.getText().toString()));
		obj.setC0002_005_01_N(Integer.parseInt(etC2_05.getText().toString()
				.trim().length() == 0 ? "0" : etC2_05.getText().toString()));
		obj.setC0002_006_01_N(Integer.parseInt(etC2_06.getText().toString()
				.trim().length() == 0 ? "0" : etC2_06.getText().toString()));
		obj.setC0002_007_01_N(Integer.parseInt(etC2_07.getText().toString()
				.trim().length() == 0 ? "0" : etC2_07.getText().toString()));
		obj.setC0002_008_01_N(Integer.parseInt(etC2_08.getText().toString()
				.trim().length() == 0 ? "0" : etC2_08.getText().toString()));
		obj.setC0002_009_01_N(Integer.parseInt(etC2_09.getText().toString()
				.trim().length() == 0 ? "0" : etC2_09.getText().toString()));
		obj.setC0002_010_01_N(Integer.parseInt(etC2_10.getText().toString()
				.trim().length() == 0 ? "0" : etC2_10.getText().toString()));
		obj.setC0002_011_01_N(Integer.parseInt(etC2_11.getText().toString()
				.trim().length() == 0 ? "0" : etC2_11.getText().toString()));
		obj.setC0002_012_01_N(Integer.parseInt(etC2_12.getText().toString()
				.trim().length() == 0 ? "0" : etC2_12.getText().toString()));
		obj.setC0002_013_01_N(Integer.parseInt(etC2_13.getText().toString()
				.trim().length() == 0 ? "0" : etC2_13.getText().toString()));

		if (nuevo) {
			if (cap2Con.Cap02Insertar(obj) > 0) {
				nuevo = false;
				ValidarJust();
				ValidarEstado();
				m.Exito("SE GRABO CON EXITO");
			} else {
				m.Error("ERROR AL GRABAR");
			}
		} else {
			if (cap2Con.Cap02Modificar(obj) > 0) {
				ValidarJust();
				ValidarEstado();
				m.Exito("SE MODIFICO CON EXITO");
			} else {
				m.Error("ERROR AL MODIFICAR");
			}
		}
	}

	private void ValidarJust() {
		int c01 = Integer
				.parseInt(etC2_01.getText().toString().length() == 0 ? "0"
						: etC2_01.getText().toString());
		int c02 = Integer
				.parseInt(etC2_02.getText().toString().length() == 0 ? "0"
						: etC2_02.getText().toString());
		int c03 = Integer
				.parseInt(etC2_03.getText().toString().length() == 0 ? "0"
						: etC2_03.getText().toString());
		int c04 = Integer
				.parseInt(etC2_04.getText().toString().length() == 0 ? "0"
						: etC2_04.getText().toString());
		int c06 = Integer
				.parseInt(etC2_06.getText().toString().length() == 0 ? "0"
						: etC2_06.getText().toString());
		int c07 = Integer
				.parseInt(etC2_07.getText().toString().length() == 0 ? "0"
						: etC2_07.getText().toString());
		int c08 = Integer
				.parseInt(etC2_08.getText().toString().length() == 0 ? "0"
						: etC2_08.getText().toString());
		int c09 = Integer
				.parseInt(etC2_09.getText().toString().length() == 0 ? "0"
						: etC2_09.getText().toString());
		int c10 = Integer
				.parseInt(etC2_10.getText().toString().length() == 0 ? "0"
						: etC2_10.getText().toString());
		int c13 = Integer
				.parseInt(etC2_13.getText().toString().length() == 0 ? "0"
						: etC2_13.getText().toString());

		if (c06 == 0 && etC2061.getText().toString().trim().length() == 0) {
			lJust.get(0).setC0JUS_001_03_N(etC2061.getText().toString());
			lJust.get(0).setFLAG("0");
		} else {
			lJust.get(0).setC0JUS_001_03_N(etC2061.getText().toString());
			lJust.get(0).setFLAG("1");
		}
		if (c13 == 0 && etC213.getText().toString().trim().length() == 0) {
			lJust.get(1).setC0JUS_001_03_N(etC213.getText().toString());
			lJust.get(1).setFLAG("0");
		} else {
			lJust.get(1).setC0JUS_001_03_N(etC213.getText().toString());
			lJust.get(1).setFLAG("1");
		}
		if (c09 == 0 && etC209.getText().toString().trim().length() == 0) {
			lJust.get(2).setC0JUS_001_03_N(etC209.getText().toString());
			lJust.get(2).setFLAG("0");
		} else {
			lJust.get(2).setC0JUS_001_03_N(etC209.getText().toString());
			lJust.get(2).setFLAG("1");
		}
		if (c10 == 0 && etC210.getText().toString().trim().length() == 0) {
			lJust.get(3).setC0JUS_001_03_N(etC210.getText().toString());
			lJust.get(3).setFLAG("0");
		} else {
			lJust.get(3).setC0JUS_001_03_N(etC210.getText().toString());
			lJust.get(3).setFLAG("1");
		}
		if ((c01 > 0 && c07 == 0)
				&& etC207.getText().toString().trim().length() == 0) {
			lJust.get(4).setC0JUS_001_03_N(etC207.getText().toString());
			lJust.get(4).setFLAG("0");
		} else {
			lJust.get(4).setC0JUS_001_03_N(etC207.getText().toString());
			lJust.get(4).setFLAG("1");
		}
		if ((c07 > 0 && c01 == 0)
				&& etC2011.getText().toString().trim().length() == 0) {
			lJust.get(5).setC0JUS_001_03_N(etC2011.getText().toString());
			lJust.get(5).setFLAG("0");
		} else {
			lJust.get(5).setC0JUS_001_03_N(etC2011.getText().toString());
			lJust.get(5).setFLAG("1");
		}
		if (((c02 + c03) > 0 && c08 == 0)
				&& etC208.getText().toString().trim().length() == 0) {
			lJust.get(6).setC0JUS_001_03_N(etC208.getText().toString());
			lJust.get(6).setFLAG("0");
		} else {
			lJust.get(6).setC0JUS_001_03_N(etC208.getText().toString());
			lJust.get(6).setFLAG("1");
		}
		if ((c08 > 0 && (c02 + c03) == 0)
				&& etC203.getText().toString().trim().length() == 0) {
			lJust.get(7).setC0JUS_001_03_N(etC203.getText().toString());
			lJust.get(7).setFLAG("0");
		} else {
			lJust.get(7).setC0JUS_001_03_N(etC203.getText().toString());
			lJust.get(7).setFLAG("1");
		}
		if ((c06 <= c13) && etC2062.getText().toString().trim().length() == 0) {
			lJust.get(8).setC0JUS_001_03_N(etC2062.getText().toString());
			lJust.get(8).setFLAG("0");
		} else {
			lJust.get(8).setC0JUS_001_03_N(etC2062.getText().toString());
			lJust.get(8).setFLAG("1");
		}
		if (((c02 + c03 + c04) <= c01)
				&& etC204.getText().toString().trim().length() == 0) {
			lJust.get(9).setC0JUS_001_03_N(etC204.getText().toString());
			lJust.get(9).setFLAG("0");
		} else {
			lJust.get(9).setC0JUS_001_03_N(etC204.getText().toString());
			lJust.get(9).setFLAG("1");
		}

		if (nuevoJust) {
			for (int i = 0; i < 10; i++) {
				jusCon.JustificacionInsertar(lJust.get(i));
			}
			nuevoJust = false;
		} else {
			for (int i = 0; i < 10; i++) {
				jusCon.JustificacionModificar(lJust.get(i));
			}
		}
	}

	private void ValidarEstado() {
		MarcoController marCon = new MarcoController(getActivity());
		Marco mar = new Marco(RUC, USUARIO);
		mar = marCon.MarcoObtener(mar);
		mar.setVALIDADO("2");
		Justificacion jus1 = new Justificacion(RUC, USUARIO, "1");
		List<Justificacion> lJus1 = jusCon.JustificacionObtenerPorRuc(jus1);
		Justificacion jus2 = new Justificacion(RUC, USUARIO, "2");
		List<Justificacion> lJus2 = jusCon.JustificacionObtenerPorRuc(jus2);
		Justificacion jus3 = new Justificacion(RUC, USUARIO, "3");
		List<Justificacion> lJus3 = jusCon.JustificacionObtenerPorRuc(jus3);
		int continuar = 1;

		if (lJus1 != null && continuar == 1) {
			for (int i = 0; i < lJus1.size(); i++) {
				if (lJus1.get(i).getFLAG().equals("0")) {
					mar.setVALIDADO("1");
					continuar = 0;
					break;
				}
			}
		}
		if (lJus2 != null && continuar == 1) {
			for (int i = 0; i < lJus2.size(); i++) {
				if (lJus2.get(i).getFLAG().equals("0")) {
					mar.setVALIDADO("1");
					continuar = 0;
					break;
				}
			}
		}
		if (lJus3 != null && continuar == 1) {
			for (int i = 0; i < lJus3.size(); i++) {
				if (lJus3.get(i).getFLAG().equals("0")) {
					mar.setVALIDADO("1");
					continuar = 0;
					break;
				}
			}
		}

		marCon.MarcoActualizar(mar);
	}
}
